package com.webservice.mail;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Locale;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.webservice.store.DataAdapter;



import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.SimpleHash;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class MailSender 
{
	private static String SMTP_HOST_NAME ;
	private static String SMTP_PORT ;
	private static String emailMsgTxt ;
	private static String emailSubjectTxt ;
	private static String emailFromAddress ;
	private static String emailFromPassword ;
	private static final String PROP_FILE="mail.properties";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	public static String  PROP_SUPPORT_NAME	;
	
	static Properties props = new Properties();
	private void postMailtoRegisteredUser(String from,String teacherName,String sessionID,String password,String designation,String recipient, String subject,String ftlfileurl,String ftl)throws MessagingException
	{
		try
		{
			boolean debug = false;
			Session session = Session.getInstance(props, new GMailAuthenticator(emailFromAddress,emailFromPassword)); 
		    session.setDebug(debug);
		    Message msg = new MimeMessage(session);
		    Template t = null;
			Configuration cfg = new Configuration();
		
			cfg.setDirectoryForTemplateLoading(new File(ftlfileurl));
			cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
			cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
			cfg.setDefaultEncoding("ISO-8859-1");
			cfg.setOutputEncoding("UTF-8");
			//default locale
			cfg.setLocale(Locale.US);
			t = cfg.getTemplate(ftl);
			SimpleHash rootcontent = new SimpleHash();
			rootcontent.put("teacherName",teacherName);
			rootcontent.put("sessionID",sessionID);
			rootcontent.put("password",password);
			rootcontent.put("designation",designation);
			
			final StringWriter writer = new StringWriter();
			t.process(rootcontent, writer);
		    InternetAddress addressFrom = new InternetAddress(from);
		    msg.setFrom(addressFrom);
		    Multipart htmlContent = new MimeMultipart("related");
			BodyPart htmlPage = new MimeBodyPart();

			htmlPage.setDataHandler(new DataHandler(new DataSource() 
			{
				public String getContentType() 
				{					
					return "text/html;charset=\"ISO-8859-1\"";
				}

				public InputStream getInputStream() throws IOException
				{
					ByteArrayInputStream bais = new ByteArrayInputStream(writer.toString().getBytes("ISO-8859-1"));
					return bais;
				}

				public String getName() 
				{
					return "main";
				}

				public OutputStream getOutputStream() throws IOException 
				{
					throw new IOException();
				}
			}));
			htmlContent.addBodyPart(htmlPage);
			
			 InternetAddress addressTo = new InternetAddress(); 
			 addressTo = new InternetAddress(recipient);
			 msg.setRecipient(Message.RecipientType.TO, addressTo);
		     msg.setSubject(subject);
		     // msg.setContent(message, "text/plain");
		     msg.setContent(htmlContent);
		     Transport.send(msg);

		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static void ReadProperty()
	{
		try
		{
			InputStream is = DataAdapter.class.getResourceAsStream(PROP_FILE);
			props.load(is);			
			props.put("mail.smtp.socketFactory.class", SSL_FACTORY);			
			
			SMTP_HOST_NAME = props.getProperty("mail.host"); 
			props.put("mail.smtp.auth", "true");
			props.put("mail.debug", "true");
			
			SMTP_PORT = props.getProperty("mail.port");
			props.put("mail.smtp.socketFactory.port", SMTP_PORT);
			
			emailFromAddress = props.getProperty("mail.emailFromAddress");
			props.put("mail.emailFromAddress", emailFromAddress);
			
			emailFromPassword = props.getProperty("mail.emailFromPassword");
			props.put("mail.emailFromPassword", emailFromPassword);
			
			emailMsgTxt = props.getProperty("mail.emailMsgTxt");
			props.put("mail.emailMsgTxt", emailMsgTxt);				
			
			emailSubjectTxt = props.getProperty("mail.emailSubjectTxt");
			props.put("mail.emailSubjectTxt", emailSubjectTxt);
					
			PROP_SUPPORT_NAME = props.getProperty("mail.support.name");
			props.put("mail.support.name", PROP_SUPPORT_NAME);
	
			is.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	public void doMaiilProcessforRegisteredUser(String teacherName,String sessionID,String password,String designation,String email, String ftlfileurl,String ftl) 
	{
		ReadProperty();
		try 
		{
			postMailtoRegisteredUser(emailFromAddress,teacherName,sessionID,password,designation,email,emailMsgTxt,ftlfileurl,ftl);
		}
		catch (MessagingException e) 
		{
			e.printStackTrace();
		}		
	}
		
}
